<?php
class Mod_counter extends CI_Controller{
    private $counter_expire = 600; // Time expire in one visit
    function __construct(){
        parent::__construct();
    }
    function index(){
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $datenow = date('Y-m-d H:i:s');
        $wheredate = array(
            'date'      => date('z'),
            'week'      => date('W'),
            'month'     => date('n'),
            'year'      => date('Y')
        );
        // 
        $counter_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? addslashes(trim($_SERVER['HTTP_USER_AGENT'])) : "";
        $counter_ip = trim(addslashes($_SERVER['REMOTE_ADDR']));
        
        // ignorore some bots
        //if (substr_count($counter_agent, "bot") > 0)
        //   $ignore = true;
        // delete free counter_ips
        $where = "unix_timestamp(NOW())-unix_timestamp(timevisit) > $this->counter_expire";
        $this->common_model->deletedata('counter_visit',$where);

        // check for entry
        $res = $this->common_model->getnumber2('counter_visit',array('guestip'=>$counter_ip));
        if ($res == 0){
            $this->common_model->insertdata('counter_visit',array('guestip'=>$counter_ip,'timevisit'=> $datenow));
        }else{
            $this->common_model->updatedata('counter_visit',array('guestip'=>$counter_ip),array('timevisit'=>$datenow ) );
        }
        $dataday = $this->common_model->getone('counter_values',$wheredate);
        if($dataday){
            if(empty($_COOKIE['counter'])){
                $dataday->guests ++;
                $this->common_model->updatedata('counter_values',$wheredate,$dataday);
                setcookie("counter","OK",time()+8600);
            }
            
        }else{ // Neu day la lan truy cap dau tien cua ngay thi tao mot bang
            $dataday = $wheredate;
            $this->common_model->insertdata('counter_values',$dataday);
        }
        $where_yesterday = array('date'=>(date('z')-1),'week'=>date('W'),'month'=>date('n'),'year'=>date('Y'));
        $where_day = array('date'=>date('z'),'week'=>date('W'),'month'=>date('n'),'year'=>date('Y'));
        $where_week = array('week'=>date('W'),'month'=>date('n'),'year'=>date('Y'));
        $where_month = array('month'=>date('n'),'year'=>date('Y'));
        $where_year = array('year'=>date('Y'));
        
        $data['counter_now'] = $this->common_model->getnumber2('counter_visit','');
        $data['counter_day'] = $this->common_model->getsum('guests',$where_day,'counter_values');
        $data['counter_yesterday'] = $this->common_model->getsum('guests',$where_yesterday,'counter_values');
        $data['counter_week'] = $this->common_model->getsum('guests',$where_week,'counter_values');
        $data['counter_month'] = $this->common_model->getsum('guests',$where_month,'counter_values');
        $data['counter_year'] = $this->common_model->getsum('guests',$where_year,'counter_values');
        $data['counter_all'] = $this->common_model->getsum('guests',array(),'counter_values');
        $this->load->view('mod_counter/counter',$data);
    }
}